package com.choosefine.it.basedata.service;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.dao.UserMgtMapper;
import com.choosefine.it.basedata.model.User;
import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.basedata.model.UserMgt;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(value="userMgtService")
public class UserMgtService extends BaseService{
	Logger logger = LoggerFactory.getLogger(UserMgtService.class);
	
	@Autowired
	public UserMgtMapper userMgtMapper;
	
	@Autowired
	public UserInfoService userInfoService;
	
	@Autowired
	public UserService userService;

	@Autowired
	CustomRestTemplate restTemplate;
	  
	
	@Value("#{yaml.get('basedata.userinfo.getUserByUserCode')}")
	String getUserByUserCodeURL=null;
	
	public List<UserMgt> selectUserMgtList(PageDTO pageDTO){
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return userMgtMapper.selectUserMgtList();
	}
	public List<UserMgt> selectUserMgtBy(Map<String ,Object>map,PageDTO pageDTO){
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return userMgtMapper.selectUserMgtBy(map);
	}
	public Integer updateUserMgt (String userCode,Integer useStatus,String operator){
		Map<String,Object> map = new HashMap<>();
		map.put("userCode", userCode);
		map.put("operator", operator);
		map.put("useStatus", useStatus);
		return userMgtMapper.updateUserMgt(map);
	}
	
	public Integer addUserMgt (UserInfo userinfo,Integer useStatus,String operator) throws BusinessException{
		Map<String,Object>map= new HashMap<>();
		User user = new User();
		user.setIsValid(userinfo.getIsValid());
		user.setUserName(userinfo.getRealName());
		user.setUserCode(userinfo.getUserCode());
		user.setUserType(0);
		user.setCtime(userinfo.getCtime());
		userService.insert(user);
		userInfoService.insert(userinfo);
		map.put("userCode", userinfo.getUserCode());
		map.put("operator",operator );
		map.put("useStatus", useStatus);
		return userMgtMapper.addUserMgt(map);
	}
	
	public Integer deleteUserMgt(String userCodes){
//		String url = deleteUserinfoURL;
//		logger.info("deleteUserinfoURL="+deleteUserinfoURL);
//		restTemplate.delete(url, userCode);
		return userInfoService.deleteByUserCode(userCodes);
	}
	public UserInfo selectUserMgtByUserCode(String userCode){
		String url = URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  getUserByUserCodeURL).append(userCode).toString();
		return restTemplate.getForObject(url, UserInfo.class);
	}
	
}
